<?php

namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class StudentsysController extends AdminbaseController {
    public function index() {
       
        $adminName=$_SESSION['name'];
        $exportFrom=$adminName;
        
        
        if (!isset($_SESSION['activequ'])){
            $_SESSION['activequ']='all';
        }
        if (isset($_GET['qu'])){
            $_SESSION['activequ']=$_GET['qu'];
        }
        $p=1;
        if (isset($_GET['p'])){
            $p=$_GET['p'];
        }
        $queryDesc='order by id desc';
        $idDesc='asc';
        if (isset($_GET['orrule'])){
            if ('asc' == $_GET['orrule']){
                $queryDesc='order by id asc';
                $idDesc='desc';
            }
        }
        $postData=array();
        $postData['name']=$postData['phone']=$postData['hao']='';
        $where=NULL;
        if(isset($_POST['name'])){
            $postData['name']=$_POST['name'];
            $postData['phone']=$_POST['phone'];
            $postData['hao']=$_POST['xjh'];
            if (!empty($_POST['name'])){
                $where.=" name like '%".trim($_POST['name'])."%' ";
            }
            if (!empty($_POST['phone'])){
                if (!empty($where)){
                    $where.=" and ";
                }
                $where.=" phone='".trim($_POST['phone'])."' ";
            }
            if (!empty($_POST['xjh'])){
                if (!empty($where)){
                    $where.=" and ";
                }
                $where.=" hao='".trim($_POST['xjh'])."' ";
            }
            
            if (!empty($where)){
                $where=' where '.$where;
            }
            
        }
        
        if (empty($where) && 'all' != $_SESSION['activequ']){
            $where=" where qu='".$_SESSION['activequ']."区' ";
        }
        

          
        $postData['idDesc']=$idDesc;
        $postData['p']=$p;
        $postData['activequ']=$_SESSION['activequ'];
        $postData['adminName']=$adminName;
        $postData['exportFrom']=$_SESSION['activequ'];
   
        
        $Model = M();
        $query=$Model->query("SELECT count(id) as count FROM `app_student` ".$where."  ");
        $count=$query[0]['count'];
        $page = $this->page($count, 10, $p);
        $studentInfo=$Model->query("SELECT * FROM `app_student` ".$where."  ".$queryDesc." limit ".(($p-1)*10).",10 ");
        $quData=json_decode(file_get_contents('./c/qu.txt'), true);
        
        $this->assign("postData", $postData);
        $this->assign("page", $page->show('Admin'));
        $this->assign("quData", $quData);
        $this->assign("studentInfo", $studentInfo);
        
        
        $this->display();
    }
    
    public function clean() {
        
        if (isset($_GET['id'])){
            $id=$_GET['id'];
        }else{
            exit;
        }
        
        $Model = M();
        $query=$Model->execute("update `app_student` SET `idcard` = NULL, phone=NULL  where id='".$id."'    ");
 
        header("Location: admin.php?g=Admin&m=Studentsys&a=index");
        exit;
    
    }
    public function edit() {
        if (isset($_GET['id'])){
            $id=$_GET['id'];
        }else{
            exit;
        }
        $p=1;
        if (isset($_GET['p'])){
            $p=$_GET['p'];
        }
        $Model = M();
        $studentInfo=$Model->query("SELECT * FROM `app_student` where id='".$id."' limit 0,1  ");
        $this->assign("p", $p);
        $this->assign("student", $studentInfo[0]);
        $this->display();
    }
     
    public function editpost() {
        if (isset($_POST['name']) && isset($_POST['option_id'])){
            $student = M("student", 'app_');
            $data['name'] = addslashes(trim($_POST['name']));
            $data['idcard'] = addslashes(trim($_POST['idcard']));
            $data['phone'] = addslashes(trim($_POST['phone']));
            $id=$_POST['option_id'];
            $student->where('id='.$id)->save($data);
            $info=json_encode(array('status'=>'yes'));
            echo $info;
        }
    }
 
}
